package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.MenuMapper;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.vo.MenuVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageData=null;
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        if (queryPageBean.getQueryString()==null){
            pageData=page(new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()));
        }else {
            wrapper.like("NAME",queryPageBean.getQueryString());
            pageData=page(new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),wrapper);
        }
        return new PageResult(pageData.getTotal(),pageData.getRecords());
    }

    @Override
    public Boolean deleteById(int id) {
        return baseMapper.deleteById(id)==1;
    }

    @Override
    public List<MenuVO> findMenuTree() {
        //查询一级菜单
        List<MenuVO> menuVOList=baseMapper.findFirstMenu();
        //遍历添加二级菜单
        for (MenuVO vo : menuVOList) {
            List<MenuVO> children=baseMapper.findSecondMenu(vo.getId());
            vo.setChildren(children);
        }
        return menuVOList;
    }
}
